package com.oracle.team.view;

import com.oracle.team.domain.*;
import com.oracle.team.service.*;

public class TeamView {
    private NameListService listSvc = new NameListService();
    private TeamService teamSvc = new TeamService();

    public void enterMainMenu() {
        boolean loopFlag = true;
        char key = 0;

        do {
            if (key != '1') {
            	listAllEmployees();
            }
            
            System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)：");
            key = TSUtility.readMenuSelection();
            System.out.println();
            switch (key) {
                case '1':
                    listTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    System.out.print("确认是否退出(Y/N)：");
                    char yn = TSUtility.readConfirmSelection();
                    if (yn == 'Y') loopFlag = false;
                    break;
            }
        } while (loopFlag);
        
        /*for(; loopFlag; ) {
        	
        }*/
    }

    private void listAllEmployees() {
        System.out.println("\n-------------------------------开发团队调度软件--------------------------------\n");
        Employee[] emps = listSvc.getAllEmployees();
        
        System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
        for(Employee emp : emps) {
        	System.out.println(emp.getListDetails());
        }
        
        System.out.println("-------------------------------------------------------------------------------");
    }

    private void listTeam() {
        System.out.println("\n--------------------团队成员列表---------------------\n");
        
        Programmer[] progs = teamSvc.getTeam();
        //System.out.println(progs.length);
        if(progs.length == 0) {
        	System.out.println("当前无团队");
        }
        else {
        	System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票");
        	for(Programmer prog : progs) {
        		System.out.println(prog.getTeamDetails());       	
        	}
        }               
        
        System.out.println("-----------------------------------------------------");
    }

    private void addMember() {
        System.out.println("---------------------团队添加成员---------------------");
        System.out.println("请输入需要添加的员工ID:");
        int id = TSUtility.readInt();
        //根据id查询员工，若存在交给addMember方法传递参数
        Employee emp;
		try {
			emp = listSvc.getEmployee(id);
			teamSvc.addMember(emp);
		} 
		catch (TeamException e) {
			System.out.println("增加失败，原因：" + e.getMessage());
		}        
        
        TSUtility.readReturn();
    }

    private void deleteMember() {
        System.out.println("---------------------团队删除成员---------------------");
        System.out.print("请输入要删除员工的TID：");
        int id = TSUtility.readInt();
        System.out.print("确认是否删除(Y/N)：");
        char yn = TSUtility.readConfirmSelection();
        
        if (yn == 'N') {
        	return;
        }
        else {
        	try {
	            teamSvc.removeMember(id);
	            System.out.println("删除成功");
        	} 
        	catch (TeamException e) {
        		System.out.println("删除失败，原因：" + e.getMessage());
        	}
        }        

        TSUtility.readReturn();
    }

    public static void main(String[] args) {
    	TeamView teamView = new TeamView();
        teamView.enterMainMenu();
    }
}